package parameterTraining;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import main.U;
import mt.BaiduMT;
import mt.GoogleMT;
import mt.YoudaoMT;
import strategy.FeatureCalc;
import strategy.Strategy;

public class ObjectiveFunction {

	ArrayList<Feature> gf, yf, bf;	//record features
	Vector<String> Gres,Yres,Bres;	//record translation results
	Vector<String> srcSentences;
	public ObjectiveFunction() throws Exception{
		
		Gres =U.file2vector("./data/mtRes/google.txt");
		Yres =U.file2vector("./data/mtRes/youdao.txt");
		Bres =U.file2vector("./data/mtRes/baidu.txt");
		srcSentences =U.file2vector("./data/src.txt");
		
		assert(Gres.size() == Yres.size() &&Yres.size() ==Bres.size());
		
		gf =new ArrayList<Feature>();
		yf =new ArrayList<Feature>();
		bf =new ArrayList<Feature>();
		
		
		Vector<String> gv =U.file2vector("./data/mtRes/gLogP.txt");
		Vector<String> bv =U.file2vector("./data/mtRes/bLogP.txt");
		Vector<String> yv =U.file2vector("./data/mtRes/yLogP.txt");
		
		
		for(int i=0;i<gv.size();i++){
			
			HashMap<String, String> translations=new HashMap<String, String>();
			translations.put(GoogleMT.label, Gres.get(i));
			translations.put(BaiduMT.label, Bres.get(i));
			translations.put(YoudaoMT.label, Yres.get(i));
			HashMap<String, Double> bleus =FeatureCalc.getBLEU(translations);
			
			gf.add(new Feature(0.971, Double.parseDouble(gv.get(i)),bleus.get(GoogleMT.label)));		
			bf.add(new Feature(-0.486,Double.parseDouble(bv.get(i)),bleus.get(BaiduMT.label)));
			yf.add(new Feature(-0.486, Double.parseDouble(yv.get(i)),bleus.get(YoudaoMT.label)));
		}
		
		
		
	}


	public double getValue(double[] parameterWeights) throws Exception {

		double logPWeight = parameterWeights[0];
		double isGoogleWeight = parameterWeights[1];
		
		Vector<String> strategyRes =new Vector<String>();
		double gcount =0;
		double bcount =0;
		double ycount =0;
		
		
		
		for(int i=0;i<gf.size();i++){
			double gScore = getScore(gf,i,logPWeight,isGoogleWeight);
			double bScore = getScore(bf,i,logPWeight,isGoogleWeight);
			double yScore = getScore(yf,i,logPWeight,isGoogleWeight);
			
			
			if(gScore>= bScore && gScore>= yScore){
				strategyRes.add(Gres.get(i));
				gcount++;
			}else if (bScore>= yScore){
				strategyRes.add(Bres.get(i));
				bcount++;
				System.out.println("----b");
				System.out.println(srcSentences.get(i));
				System.out.println(Gres.get(i));
				System.out.println(Bres.get(i));
				System.out.println(Yres.get(i));
				System.out.println("g\t"+gf.get(i).isGoogle+"\t"+gf.get(i).N_logP);
				System.out.println("b\t"+bf.get(i).isGoogle+"\t"+bf.get(i).N_logP);
				System.out.println("y\t"+yf.get(i).isGoogle+"\t"+yf.get(i).N_logP);
				System.out.println("----");
			}else{
				strategyRes.add(Yres.get(i));
				ycount++;
				System.out.println("----y");
				System.out.println(srcSentences.get(i));
				System.out.println(Gres.get(i));
				System.out.println(Bres.get(i));
				System.out.println(Yres.get(i));
				System.out.println("g\t"+gf.get(i).isGoogle+"\t"+gf.get(i).N_logP);
				System.out.println("b\t"+bf.get(i).isGoogle+"\t"+bf.get(i).N_logP);
				System.out.println("y\t"+yf.get(i).isGoogle+"\t"+yf.get(i).N_logP);
				System.out.println("----");
			}
		}
		
		double sum = gcount +bcount+ycount;
		System.out.println("google: "+(gcount/sum));
		System.out.println("baidu: "+(bcount/sum));
		System.out.println("youdao: "+(ycount/sum));
		double bleu = Strategy.fromVectorToBleu(strategyRes);
		System.out.println("bleu: "+bleu);
		return bleu;
	}

	private double getScore(ArrayList<Feature> f, int i, double logPWeight, double isGoogleWeight) {
		return isGoogleWeight * f.get(i).isGoogle + logPWeight * f.get(i).N_logP +0.2*f.get(i).bleu;
	}


	class Feature{
		public double isGoogle;
		public double N_logP;	//normalized logP
		public double bleu;
		public Feature(double _isGoogle, double _N_logP, double _bleu){
			isGoogle=_isGoogle;
			N_logP =_N_logP;
			bleu =_bleu;
		}
	}
	


}
